package com.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.common.utils.ConvertUtils;
import com.mall.product.dao.ProductAttrValueDao;
import com.mall.product.dto.AttrDTO;
import com.mall.product.dto.ProductAttrValueDTO;
import com.mall.product.entity.AttrEntity;
import com.mall.product.entity.ProductAttrValueEntity;
import com.mall.product.service.AttrService;
import com.mall.product.service.ProductAttrValueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * spu属性值
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Service
public class ProductAttrValueServiceImpl extends CrudServiceImpl<ProductAttrValueDao, ProductAttrValueEntity, ProductAttrValueDTO> implements ProductAttrValueService {

    @Resource
    private AttrService attrService;

    @Override
    public QueryWrapper<ProductAttrValueEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void BatchSave(Long spuId,List<AttrDTO> baseAttrs) {
        if (baseAttrs!=null&& baseAttrs.size()>0){
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity productAttrValueDTO = new ProductAttrValueEntity();
                productAttrValueDTO.setAttrId(attr.getAttrId());
                AttrEntity attrEntity = attrService.selectById(attr.getAttrId());
                productAttrValueDTO.setAttrName(attrEntity != null ? attrEntity.getAttrName() : "");
                productAttrValueDTO.setAttrValue(attr.getAttrValues());
                productAttrValueDTO.setQuickShow(attr.getShowDesc());
                productAttrValueDTO.setSpuId(spuId);
                return productAttrValueDTO;
            }).collect(Collectors.toList());
            this.insertBatch(collect);
        }
    }

    @Override
    public List<ProductAttrValueDTO> queryBySpuId(Long spuId) {
        List<ProductAttrValueEntity> selectList = baseDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return ConvertUtils.sourceToTarget(selectList,ProductAttrValueDTO.class);
    }


    @Override
    public void batchUpdateAttr(List<ProductAttrValueDTO> dto, Long spuId) {
        // 删除之前的属性
        baseDao.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        // 更新
        List<ProductAttrValueEntity> entities = dto.stream().map(item -> {
            ProductAttrValueEntity entity = new ProductAttrValueEntity();
            BeanUtil.copyProperties(item, entity);
            entity.setSpuId(spuId);
            return entity;
        }).collect(Collectors.toList());
        this.insertBatch(entities);
    }

    @Override
    public List<ProductAttrValueEntity> queryAttrIdsBySpuId(Long spuId) {
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId);
        return baseDao.selectList(wrapper);
    }
}